/* Laboratorio # 2 - Administración de bases de datos
 Jorge Fallas Ramos         402090469
 José Roberto Sánchez       115400070
 Daniel Villalobos Venegas  402110100
 */
package monitorinitdb;

import java.io.*;
import monitorinitdb.Vista.VentConfig;
import monitorinitdb.Vista.VentanaArchiveLog;

public class escritoraSQL {

    public String ruta = ".";//por ahora para mantener el nombre y ruta fijos  
    File f, guardado;

    public escritoraSQL(String ruta) {
        guardado = new File(ruta);
        this.ruta = ruta;
    }

    public void escribir(String hilera, String nomArchivo) {
        f = new File(ruta);
        try {
            FileWriter w = new FileWriter(f);
            BufferedWriter bw = new BufferedWriter(w);
            PrintWriter wr = new PrintWriter(bw);
            wr.write(hilera);
            wr.close();
            bw.close();
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public String getEjecucion() {
        if (guardado != null) {
            return guardado.getAbsolutePath();
        }
        return null;
    }

    public String getRuta() throws Exception {
        if (guardado != null) {
            return guardado.getAbsolutePath();
        }
        throw new Exception("ERROR de archivo");
    }

    public void creaBD(String user, String contras, String rutaInit, String rutaBuild, String rutas, boolean archive) {
        String hilera = "connect " + user + "/" + contras + " as sysdba\n"
                + "shutdown abort;\n"
                + "startup nomount pfile= \"" + rutaInit + "\";\n"
                + "@" + rutaBuild + "\n";
        if (archive) {
            try {
                hilera += "startup pfile= \"" + rutaInit + "\";\n" + VentanaArchiveLog.getArchive() + "\n"
                        + VentConfig.getInstance().obtLogTrace() + ";";
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
            }
        }
        hilera += rutas;
        escribir(hilera, "ejecucion.sql");
    }

    public void creaInit(Init initFile, String nomArchivo) {

        String init = "db_name='" + initFile.getDb_name() + "'"
                + "\nmemory_target= " + initFile.getMemory_target()
                + "\nprocesses = " + initFile.getProcesses()
                + "\ndb_block_size= " + initFile.getDb_block_size()
                + "\ndb_domain=''"
                + "\ndb_recovery_file_dest= " + initFile.getDb_recovery_file_dest()
                + "\ndb_recovery_file_dest_size= " + initFile.getDb_recovery_file_dest_size()
                + "\ndispatchers='(PROTOCOL=TCP) (SERVICE=ORCLXDB)'"
                + "\nopen_cursors=500"
                + "\nremote_login_passwordfile='EXCLUSIVE'"
                + "\nundo_tablespace= '" + initFile.getUndo_tablespace() + "'"
                + "\ncontrol_files = " + initFile.getControl_files()
                + "\ncompatible = " + initFile.getCompatible();
        if (!initFile.getLog_checkpoint_interval().equals("")) {
            init += "\nlog_checkpoint_interval = " + initFile.getLog_checkpoint_interval();
        }
        if (!initFile.getLOG_CHECKPOINT_TIMEOUT().equals("")) {
            init += "\nLOG_CHECKPOINT_TIMEOUT = " + initFile.getLOG_CHECKPOINT_TIMEOUT();
        }
        escribir(init, nomArchivo);
    }

    public void creaBuild(Build buildFile, String nomArchivo) {

        String build = "ALTER SYSTEM SET DB_CREATE_FILE_DEST = '" + buildFile.getDB_CREATE_FILE_DEST() + "';"
                + "\ncreate database \"" + buildFile.getDatabase() + "\""
                + "\nlogfile " + buildFile.getLogfiles()
                + "\n" + buildFile.getCharacter()
                + "\n" + buildFile.getNationalCharacter()
                + "\n" + buildFile.getDataFile()
                + "\n" + buildFile.getSysaux_datafile()
                + "\n" + buildFile.getUndo_tablespace();

        if (buildFile.getTablespaces() != null) {
            build += "\n" + buildFile.getTablespaces();
        }

        if (buildFile.getLOG_ARCHIVE_TRACE() != null) {
            build += "\n" + buildFile.getLOG_ARCHIVE_DEST_1()
                    + "\n" + "ALTER DATABASE CLOSE;"
                    + "\n" + buildFile.getAlter_database_archivelog()
                    + "\nALTER SYSTEM SET archive_lag_target =" + buildFile.getArchive_lag_target() + ";"
                    + "\nSHUTDOWN IMMEDIATE;";
        }

//                build+= "\n" + buildFile.getDataBaseMount()
//                + "\n" + buildFile.getDatabase_open();
        escribir(build, nomArchivo);
    }
}
